<?php
/**
 *  Evoca Media Services REST API Implementation
 *
 *  This a generic PHP class that can call Evoca's Media Services.  Most of the functions return
 *  an eac_curl http request object.  The common methods on this object are:
 *  
 *  getLastResult() - returns the result of the last request (the response body)
 *  getInfo() - returns a status array of the last request
 *  getHeaders() - returns the response headers of the last request.
 *  getOptions() - returns the current options array.
 *
 *  Author:Jeff Walter
 *  http://www.evoca.com
 *
 *  This code is free, provided AS-IS with no warranty expressed or implied.  Use at your own risk.
 *
 */

require_once "EMS_util.class.php";
require_once "EMS_credentials.class.php";

class EMS_api {

	private $EMS_util = null;

	/**
	 * Constructor: Evoca Media Services REST API implementation
	 */
	public function EMS_api() {
		$this->EMS_util = new EMS_util(EMS_credentials::clientID, EMS_credentials::publicID, EMS_credentials::secretKey);
	}

	/**
	 * Makes a request for the Evoca Playlist Player.  Returns the result of the request in an eac_curl http request object.
	 * 
	 * Some valid methods on the returned object:
	 * getHeaders() - returns an array of the response headers
	 * getLastResult() - returns the body of the response (in this case, the HTML required to display the playlist player)
	 * 
	 * @return an eac_curl http object
	 * @param $playername Object the name of the player (a list can be found in the Configuration->Players portion of the EMS website)
	 * @param $context Object[optional] the context (or context + filename) to pull files from.  Must start with a "/".
	 * @param $filter Object[optional]  a metadata filter (e.g. publish=true)
	 * @param $orderBy Object[optional] an ordering directive (e.g. publishDate;DESC)
	 * @param $maxResults Object[optional] the maximum results to return (defaults to 10 if not specified, but can be no greater than 100)
	 */
	public function get_playlist_player($playername, $context = "/", $filter = null, $orderBy = null, $maxResults = null) {
		// Ensure that a blank context/filename translates to a "/"
		if($context == "")
			$context = "/";
			
		$param_array = array();

		if($filter != null)
			$param_array["filter"] = $filter;
		if($orderBy != null)
			$param_array["orderBy"] = $orderBy;
		if($maxResults != null)
			$param_array["maxResults"] = $maxResults;

		$paramstring = $this->EMS_util->generate_query_string($param_array);

		$resourcestring = "/players/audio/flash/$playername$context$paramstring";

		return $this->EMS_util->ems_get($resourcestring);
	}

	/**
	 * Makes a request for the Evoca Recorder.  Returns the result of the request in an eac_curl http request object.
	 * 
	 * Some valid methods on the returned object:
	 * getHeaders() - returns an array of the response headers
	 * getLastResult() - returns the body of the response (in this case, the HTML required to display the playlist player)
	 * 
	 * @return an eac_curl http object
	 * @param $recordername Object the name of the recorder (a list can be found in the Configuration->Recorder portion of the EMS website)
	 * @param $context Object[optional] the context to save files to.  Must start with a "/".
	 * @param $wmode Object[optional]  the wmode value for the flash to come back with (defaults to "transparent")
	 * @param $saveurl Object[optional] the url that the save POST should go to
	 */
	public function get_recorder($recordername, $context = "/", $wmode = null, $saveurl = null) {
		// Ensure that a blank context/filename translates to a "/"
		if($context == "")
			$context = "/";
			
		$param_array = array();

		if($context != null)
			$param_array["context"] = $context;
		if($wmode != null)
			$param_array["wmode"] = $wmode;
		if($saveurl != null)
			$param_array["saveurl"] = $saveurl;

		$paramstring = $this->EMS_util->generate_query_string($param_array);

		$resourcestring = "/recorders/audio/flash/$recordername$paramstring";

		return $this->EMS_util->ems_get($resourcestring);
	}
	
	/**
	 * Makes a request for the metadata of an EMS file (recording).  Returns the result of the request in an eac_curl http request object.
	 * 
	 * Some valid methods on the returned object:
	 * getHeaders() - returns an array of the response headers
	 * getLastResult() - returns the body of the response (in this case, the XML representation of the file metadata)
	 * 
	 * @return an eac_curl http object containing the XML metadata representation
	 * @param $resource Object The file resource URL (e.g. /phoneRecordings/1h2din3fscjvdk33dkdncd).  It must start with a leading "/".
	 * @param $keys Object[optional] The comma separated list of metadata to return.  Pass the empty string to return all metadata.
	 */
	public function get_file_metadata($resource, $keys = null) {
		$resourceString = $resource . "/metadata";
		$param_array = array();
		
		if($keys!= null) {
			// If we want to include all metadata, add the includeMetadata parameter with no value
			if($keys == "") {
				$param_array["includeMetadata"] = "";
			}
			else {
				$param_array["includeMetadata"] = $keys;
			}
		}
		return $this->EMS_util->ems_get($resourceString . $this->EMS_util->generate_query_string($param_array));
	}
	
	/**
	 * Makes a request to set the metadata of an EMS file (recording).  Returns the result of the request in an eac_curl http request object.
	 * 
	 * Some valid methods on the returned object:
	 * getHeaders() - returns an array of the response headers
	 * getInfo() - returns the body of the response (in this case, the XML representation of the file metadata)
	 * 
	 * @return an eac_curl http object containing the response status
	 * @param $resource Object The file resource URL (e.g. /phoneRecordings/1h2din3fscjvdk33dkdncd).  It must start with a leading "/".
	 * @param $metadataName String The name of the metadata value being set.
	 * @param $metadataValue String The value of the metadata to set.
	 */
	public function set_file_metadata($resource, $metadataName, $metadataVaue) {
		$resourceString = $resource . "/metadata/" . $metadataName;
		$param_array = array();
		
		if($metadataVaue != null) {
			if($metadataVaue != "") {
				$param_array["value"] = $metadataVaue;
			}
		}
		return $this->EMS_util->ems_request("PUT", $resourceString . $this->EMS_util->generate_query_string($param_array));
	}
}
?>